<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class OrderRefund extends Model
{
    use SoftDelete;

    protected $name = 'order_refunds';

    protected $autoWriteTimestamp = 'datetime';

    protected $append = ['status_label'];

    public const STATUS_PENDING = 0;
    public const STATUS_APPROVED = 1;
    public const STATUS_REJECTED = 2;
    public const STATUS_COMPLETED = 3;

    public static function statusMap(): array
    {
        return [
            self::STATUS_PENDING => '申请中',
            self::STATUS_APPROVED => '已通过',
            self::STATUS_REJECTED => '已拒绝',
            self::STATUS_COMPLETED => '已完成',
        ];
    }

    public static function labelForStatus(int $status): string
    {
        return self::statusMap()[$status] ?? '未知状态';
    }

    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }

    public function item()
    {
        return $this->belongsTo(OrderItem::class, 'order_item_id', 'id');
    }

    public function getStatusLabelAttr(): string
    {
        return self::labelForStatus((int) $this->status);
    }
}
